import axios from 'axios'
import { Message } from 'element-ui'
import store from '@/store'

import { getToken } from '@/utils/auth'
// create an axios instance
const service = axios.create({
  baseURL: 'https://testgateway.bocai.life', // url = base url + request url
  timeout: 20 * 1000 // request timeout
})

service.interceptors.request.use(config => {
  if (store.getters.token) {
    // config.headers['Auth-Token'] = getToken()
    config.headers.authorization = getToken()
    config.headers['Content-Type'] = 'application/json'
  }
  return config
})

service.interceptors.response.use(
  response => {
    let res = response.data
    if ((res.code !== 0) && (res.code !== '0000')) {
      Message({
        message: res.message || 'Error',
        type: 'error',
        duration: 5 * 1000
      })
    } else {
      return res.data
    }
  }
)
export default service
